$(function () {
  /* -------------渲染信息到页面----------- */
  //过滤接口自带时间
  template.defaults.imports.time = function (date) {
    let dt = new Date(date);
    // console.log(dt);
    let y = dt.getFullYear();
    let m = zeroPadding(dt.getMonth());
    let r = zeroPadding(dt.getDate());

    let h = zeroPadding(dt.getHours());
    let min = zeroPadding(dt.getMinutes());
    let s = zeroPadding(dt.getSeconds());
    return y + "-" + m + "-" + r + " " + h + ":" + min + ":" + s;
  };
  //时间补零
  function zeroPadding(time) {
    return time > 9 ? time : "0" + time;
  }
  //声明想要获取的参数
  let q = {
    pagenum: 1,
    pagesize: 2,
    cate_id: "",
    state: "",
  };
  artlist();
  function artlist() {
    $.ajax({
      type: "get",
      url: "/my/article/list",
      data: q,
      // dataType: "dataType",
      success: function (res) {
        // console.log(res);
        if (res.status != 0) return layui.layer.msg(res.message);
        layui.layer.msg(res.message);
        let html = template("artlist", res);
        $("tbody").html(html);
        //分页总数需要用到总共多少条信息
        paging(res.total);
      },
    });
  }
  /* -------------分页内容----------- */
  function paging(total) {
    layui.laypage.render({
      elem: "fenye", //注意，这里的 test1 是 ID，不用加 # 号
      count: total, //数据总数，从服务端得到
      limit: q.pagesize, //当前页  从服务器获取
      limits: [2, 3, 5, 7, 10], //可选每页多少信息
      curr: q.pagenum, //初始页
      layout: ["limit", "prev", "page", "next", "skip", "refresh", "count"], //自定义分页
      jump: function (obj, first) {
        console.log(obj);
        //obj包含了当前分页的所有参数，比如：
        q.pagenum = obj.curr; //得到当前页，以便向服务端请求对应页的数据。
        q.pagesize = obj.limit; //得到每页显示的条数
        //首次不执行
        if (!first) {
          artlist();
        }
      },
    });
  }
  /* -------------渲染分类到下拉框里----------- */
  copyList();
  function copyList() {
    $.ajax({
      type: "get",
      url: "/my/article/cates",
      // data: "data",
      // dataType: "dataType",
      success: function (res) {
        // console.log(res);
        let html = template("copyls", res);
        $('[name="cate_id"]').html(html);
        layui.form.render();
      },
    });
  }
  /* -------------筛选功能的渲染----------- */
  $(".shaixuan").on("submit", function (e) {
    e.preventDefault();
    //拿到每个框的值给需要上传到服务器的参数
    q.cate_id = $('[name="cate_id"]').val();
    console.log(q.cate_id);
    q.state = $('[name="state"]').val();
    console.log(q.state);
    artlist();
  });
  /* -------------删除功能----------- */
  $("body").on("click", ".shan", function () {
    let len = $(".shan").length;
    console.log(len);
    layer.confirm("是否删除?", { icon: 3, title: "提示" }, function (index) {
      let id = $(".shan").attr("data-id");
      $.ajax({
        type: "get",
        url: `/my/article/delete/${id}`,
        // dataType: "dataType",
        success: function (res) {
          if (res.status != 0) return layui.layer.msg(res.message);
          layui.layer.msg(res.message);
          if (len == 1) {
            q.pagenum = q.pagenum == 1 ? 1 : q.pagenum - 1;
          }
          artlist();
          // console.log(res);
        },
      });
    });
  });

  /* -------------编辑功能----------- */
  $("body").on("click", "#gai", function () {
    //获取id
    let id = $(this).attr("data-id");
    //根据id获取文章的数据
    $.ajax({
      type: "get",
      url: `/my/article/${id}`,
      // data: "data",
      // dataType: "dataType",
      success: function (res) {
        // console.log(res);
        localStorage.setItem("amend", JSON.stringify(res.data));
        location.href = "/article/article_amend.html";
      },
    });
  });
});
